package com.gmail.mosoft521.nettyDG.util;


import com.google.common.collect.Maps;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * DateUtils
 *
 */
public class DateUtils {

    public final static int ONE_DAY_MINUTES = 1440;

    /**
     * getCurrentDate
     * @return
     */
    public static Date getCurrentDate() {
        return new Date(System.currentTimeMillis());
    }

    /**
     * getCurrentDate
     * by changhf 2015-10-27
     * @param format
     * @return
     */
    public static Date getCurrentDate(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        ParsePosition pos = new ParsePosition(0);
        Date date = formatter.parse(getCurrentStringDate(format), pos);
        return date;
    }

    /**
     * getCurrentStringDate
     * by changhf 2015-10-27
     * @param format
     * @return
     */
    public static String getCurrentStringDate(String format) {
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(new Date());
    }

    /**
     * by changhf 2015-12-16
     * 获取两个时间相差的天数
     * @param oldDate
     * @param newDate
     * @return
     */
    public static int getIntervalDays(Date oldDate, Date newDate) {
        if (oldDate.after(newDate)) {
            throw new IllegalArgumentException("时间先后顺序不对!");
        }
        //将转换的两个时间对象转换成Calendard对象
        Calendar can1 = Calendar.getInstance();
        can1.setTime(oldDate);
        Calendar can2 = Calendar.getInstance();
        can2.setTime(newDate);
        //拿出两个年份
        int year1 = can1.get(Calendar.YEAR);
        int year2 = can2.get(Calendar.YEAR);
        //天数
        int days = 0;
        Calendar can = null;
        //减去小的时间在这一年已经过了的天数
        //加上大的时间已过的天数
        days -= can1.get(Calendar.DAY_OF_YEAR);
        days += can2.get(Calendar.DAY_OF_YEAR);
        can = can1;
        for (int i = 0; i < Math.abs(year2 - year1); i++) {
            //获取小的时间当前年的总天数
            days += can.getActualMaximum(Calendar.DAY_OF_YEAR);
            //再计算下一年。
            can.add(Calendar.YEAR, 1);
        }
        return days;
    }

    /**
     * 获取int型年月日
     * @param recommendTime
     * @return
     */
    public static String getStringShortDate(Date recommendTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

        return formatter.format(recommendTime);
    }

    /**
     * 获取int型年月日
     * @return
     */
    public static int getIntCurrentDate() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

        String date = formatter.format(new Date());
        return Integer.parseInt(date);
    }

    /**
       * 获取现在时间
       * 
       * @return返回字符串格式 yyyy-MM-dd HH:mm:ss
       */
    public static String getStringDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
     * getStringAllDate
     * @return
     */
    public static String getStringAllDate() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss_SSS");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
       * 获取现在时间
       * 
       * @return 返回短时间字符串格式yyyy-MM-dd
       */
    public static String getStringDateShort() {
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
       * 获取现在时间
       * 
       * @return 返回短时间字符串格式yyyy-MM-dd
       */
    public static String getStringDateShort(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
       * 获取时间 小时:分;秒 HH:mm:ss
       * 
       * @return
       */
    public static String getTimeShort() {
        SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
        Date currentTime = new Date();
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    /**
       * 获取时间 小时分秒 HHmmss
       * 
       * @return
       */
    public static String getTimeShort(Date recommendTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("HHmmss");
        return formatter.format(recommendTime);
    }

    /**
       * 获取时间 小时分 HHmm
       * 
       * @return
       */
    public static String getStrTimeShortWithOutSecond(Date recommendTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("HHmm");
        return formatter.format(recommendTime);
    }

    /**
     * 获取时间 小时分 HHmm
     * 
     * @return
     */
    public static int getIntTimeShortWithOutSecond(Date recommendTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("HHmm");
        return Integer.parseInt(formatter.format(recommendTime));
    }

    /**
     * 获取秒时间
     * @param date
     * @return
     */
    public static long getSecondTime(Date date) {
        return date.getTime() / 1000;
    }

    /**
       * 将长时间格式字符串转换为时间 yyyy-MM-dd HH:mm:ss
       * 
       * @param strDate
       * @return
       */
    public static Date strToDateLong(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
       * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
       * 
       * @param dateDate
       * @return
       */
    public static String dateToStrLong(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
       * 将短时间格式时间转换为字符串 yyyy-MM-dd
       * 
       * @return
       */
    public static String dateToStr(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
       * 将短时间格式字符串转换为时间 yyyy-MM-dd 
       * 
       * @param strDate
       * @return
       */
    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 当前时间是否在0点10分之前
     * @param date
     * @return
     */
    public static boolean isTimesmorning(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 10);
        cal.set(Calendar.MILLISECOND, 0);
        //当前时间的秒数
        int currSecond = (int) (date.getTime() / 1000);
        //0点10分的秒数
        int second = (int) (cal.getTimeInMillis() / 1000);

        return currSecond < second;
    }

    /**
     * 毫秒转为天，时，分，秒
     * <pre>
     * 
     * </pre>
     * @param ms 毫秒数
     * @return
     */
    public static String formatTime(long ms) {
        int ss = 1000;
        int mi = ss * 60;
        int hh = mi * 60;
        int dd = hh * 24;

        long day = ms / dd;
        long hour = (ms - day * dd) / hh;
        long minute = (ms - day * dd - hour * hh) / mi;
        long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        String strDay = "" + day; //天
        String strHour = hour < 10 ? "0" + hour : "" + hour;//小时
        String strMinute = minute < 10 ? "0" + minute : "" + minute;//分钟
        String strSecond = second < 10 ? "0" + second : "" + second;//秒
        String strMilliSecond = milliSecond < 10 ? "0" + milliSecond : "" + milliSecond;//毫秒
        strMilliSecond = milliSecond < 100 ? "0" + strMilliSecond : "" + strMilliSecond;

        return strDay + "天" + strHour + "小时" + strMinute + "分钟" + strSecond + "秒";
    }

    /**
     * 获取当前时间是星期几
     * @return
     */
    public static int getWeek() {
        Date currentTime = new Date();
        Calendar cal = Calendar.getInstance();
        cal.setTime(currentTime);
        int week = cal.get(Calendar.DAY_OF_WEEK);

        switch (week) {
            case 1:
                week = 7;
                break;
            case 2:
                week = 1;
                break;
            case 3:
                week = 2;
                break;
            case 4:
                week = 3;
                break;
            case 5:
                week = 4;
                break;
            case 6:
                week = 5;
                break;
            case 7:
                week = 6;
                break;
            default:
                break;

        }
        return week;
    }

    /** 
     * 得到几天前/后的时间 
     * @param d 传入的日期
     * @param day 相隔几天 day > 0 几天后，day < 0 几天前
     * @return 
     */
    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(Calendar.DATE, now.get(Calendar.DATE) + day);
        return now.getTime();
    }

    /**
     * daysBetween
     * @param date1
     * @param date2
     * @return
     */
    public static int daysBetween(Date date1, Date date2) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date1);
        long time1 = cal.getTimeInMillis();
        cal.setTime(date2);
        long time2 = cal.getTimeInMillis();
        long betweenDays = (time2 - time1) / (1000 * 3600 * 24);

        return Integer.parseInt(String.valueOf(betweenDays));
    }

    /**
     * 获取一周的开始日期和结束日期，格式为：yyyy-MM-dd 注：周一为第一天
     * @return
     */
    public static Map<String, String> getWeekStartAndEnd() {

        Map<String, String> map = Maps.newHashMap();
        // 开始日期
        map.put("start", getCurrentMonday());
        // 结束日期
        map.put("end", getPreviousSunday());

        return map;
    }

    /**
     * 获得当前日期与本周一相差的天数
     * @return
     */
    public static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }

    /**
     * 获得当前周- 周一的日期
     * @return
     */
    public static String getCurrentMonday() {
        int mondayPlus = getMondayPlus();
        Calendar currentDate = Calendar.getInstance();
        currentDate.add(Calendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        String preMonday = dateToStr(monday);
        return preMonday;
    }

    /**
     * 获得当前周- 周日  的日期
     * @return
     */
    public static String getPreviousSunday() {
        int mondayPlus = getMondayPlus();
        Calendar currentDate = Calendar.getInstance();
        currentDate.add(Calendar.DATE, mondayPlus + 6);
        Date sunday = currentDate.getTime();
        String preMonday = dateToStr(sunday);
        return preMonday;
    }

    /**
     * 返回日期 格式为 MM月dd日, 如10月8日
     * @param day 距离今天的天数差，若是今天，值为0
     * @return
     */
    public static String getDateFormat(Integer day) {
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, day);

        String date = calendar.get(Calendar.MONTH) + 1 + "月" + calendar.get(Calendar.DAY_OF_MONTH)
                + "日";
        return date;
    }

    /**
     * long时间转date时间
     * @param time
     * @return
     */
    public static Date longTime2Date(Long time) {

        return new Date(time);
    }

    /*	public static void main(String[] args) {
    //		DateUtils.isTimesmorning(getCurrentDate());
    //		System.out.println(55499/60/60);
    //		System.out.println(getSecondTime(new Date()));
    //		
    //		System.out.println((int)Math.ceil(1.0));
    //		
    //		System.out.println(millSecondToDate(System.currentTimeMillis()));
    //	    System.out.println(dateToStrLong(getDateAfter(new Date(),19)));
    	    
    //	    System.out.println(strToDateLong("2015-05-12 17:15:44").getTime());
    //	    System.out.println(new Date().getTime());
    	    
    //	    System.out.println(getWeekStartAndEnd().get("start"));
    //	    System.out.println(getWeekStartAndEnd().get("end"));
    	    
    //	    System.out.println(getWeekStartAndEnd().get("start"));
    //	    System.out.println(getWeekStartAndEnd().get("end"));
    	   
    //	    System.out.println(getCurrentMonday());
    //	    System.out.println(getPreviousSunday());
    //	    System.out.println("--"+strToDate(getStringDateShort()));
    	    
    	    
    //	    System.out.println(getDateFormat(5));
    	    System.out.println(System.currentTimeMillis() - (0.5 * 60 * 60 * 1000) );
    	    System.out.println(System.currentTimeMillis());
    	    System.out.println(longTime2Date(System.currentTimeMillis()));
    	}*/

}
